package arisufascion.com.compone;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * @author Kelvin
 */
public class LoadMoreListView extends ListView implements OnScrollListener {

	private static final String TAG = "LoadMoreListView";
	// Listener to process load more items when user reaches the end of the list
	private OnLoadMoreListener onLoadMoreListener;
	// To know if the list is loading more items
	private boolean mIsLoadingMore = false;
	private int mCurrentScrollState;

	public LoadMoreListView(Context context) {
		super(context);
		init(context);
	}

	public LoadMoreListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		super.setOnScrollListener(this);
		init();
	}

	private void init() {
		/*
		 * GradientDrawable gradientDrawable = new GradientDrawable( ) ;
		 * gradientDrawable.setCornerRadius(0) ;
		 * gradientDrawable.setColor(0xffffffff) ;
		 * setBackgroundDrawable(gradientDrawable) ;
		 */
		setCacheColorHint(0);
		setVerticalFadingEdgeEnabled(false);		
	}

	@Override
	public void setAdapter(ListAdapter adapter) {
		super.setAdapter(adapter);
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {

		// String str = String.format("F: %d;V: %d;T: %d;more=%s; STate: %d",
		// firstVisibleItem,visibleItemCount,totalItemCount,mIsLoadingMore,mCurrentScrollState);
		if (onLoadMoreListener != null) {
			if (visibleItemCount == totalItemCount) {
				return;
			}
			boolean loadMore = (firstVisibleItem + visibleItemCount) >= totalItemCount;
			if (!mIsLoadingMore && loadMore
					&& mCurrentScrollState != SCROLL_STATE_IDLE) {
				mIsLoadingMore = true;
				onLoadMore();
			}
		}
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		mCurrentScrollState = scrollState;
	}

	public void onLoadMore() {
		Log.d(TAG, "onLoadMore");
		if (onLoadMoreListener != null) {
			onLoadMoreListener.onLoadMore();
		}
	}

	/**
	 * Notify the loading more operation has finished
	 */
	public void onLoadMoreComplete() {
		mIsLoadingMore = false;
	}

	/**
	 * Notify the loading more operation has finished
	 */
	public void onLoadFinished() {
		mIsLoadingMore = false;
	}

	/***
	 * 
	 * @param onLoadMoreListener
	 */
	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		this.onLoadMoreListener = onLoadMoreListener;
	}

	/**
	 * Interface definition for a callback to be invoked when list reaches the
	 * last item (the user load more items in the list)
	 */
	public interface OnLoadMoreListener {
		/**
		 * Called when the list reaches the last item (the last item is visible
		 * to the user)
		 */
		public void onLoadMore();
	}

}